package net.monare.mifss.admin.ui.mod;

import com.vaadin.ui.*;
import net.monare.mifss.MifssAdminMainUI;
import net.monare.mifss.MifssHolder;
import net.monare.mifss.MifssMessages;
import net.monare.mifss.admin.ui.comp.PropertyListUi;
import net.monare.mifss.admin.ui.valid.EmptyStringValidator;
import net.monare.mifss.admin.ui.valid.IntIdValidator;
import net.monare.mifss.configurations.pojo.ContentPoolDef;
import net.monare.mifss.services.StorageService;

import java.util.List;

public class PoolDefUiModule extends FormLayout {

    private static final long serialVersionUID = 5946391423363556311L;

    private TextField description;
    private TextField poolId;
    private ComboBox implName;
    private TextField title;
    private CheckBox isWriteActive;

    private PropertyListUi props;

    public PoolDefUiModule() {
        initElements();
    }

    public PoolDefUiModule(ContentPoolDef contentPoolDef) {
        initElements();
        populateElements(contentPoolDef);
    }

    public void setContentPoolDef(ContentPoolDef contentPoolDef) {
        populateElements(contentPoolDef);
    }

    public void protectFields() {
        poolId.setEnabled(false);
    }

    public ContentPoolDef getContentPoolDef() {

        ContentPoolDef poolDef = null;

        try {

            poolDef = new ContentPoolDef();

            if (description.isValid()) {
                poolDef.setDescription(description.getValue());
            } else {
                return null;
            }

            if (poolId.isValid()) {
                poolDef.setId(Integer.parseInt(poolId.getValue()));
            } else {
                return null;
            }

            if (implName.isValid()) {
                poolDef.setImplementation(implName.getValue().toString());
            } else {
                return null;
            }

            if (title.isValid()) {
                poolDef.setTitle(title.getValue());
            } else {
                return null;
            }

            poolDef.setProperties(props.getPropertySet());
            poolDef.setWriteActive(isWriteActive.getValue());

        } catch (Exception x) {

            poolDef = null;

        }

        return poolDef;

    }

    private void populateElements(ContentPoolDef contentPoolDef) {

        description.setValue(contentPoolDef.getDescription());
        poolId.setValue(Integer.toString(contentPoolDef.getId()));
        implName.setValue(contentPoolDef.getImplementation());
        title.setValue(contentPoolDef.getTitle());
        isWriteActive.setValue(new Boolean(contentPoolDef.isWriteActive()));
        props.setProperties(contentPoolDef.getProperties());

    }

    private void initElements() {

        MifssHolder mifssHolder = ((MifssAdminMainUI) UI.getCurrent()).getMifssHolder();

        StorageService storageService = mifssHolder.getStorageService();

        // load UI messages
        String poolPropertiesLabel = MifssMessages.getMessage("label.pool.properties");
        String poolDescriptionLabel = MifssMessages.getMessage("label.pool.description");
        String poolIdLabel = MifssMessages.getMessage("label.pool.id");
        String poolImplLabel = MifssMessages.getMessage("label.pool.implementation");
        String poolTitleLabel = MifssMessages.getMessage("label.pool.title");
        String poolIsWriteActiveLabel = MifssMessages.getMessage("label.pool.write.active");
        String idValidationMsg = MifssMessages.getMessage("validation.id.int");
        String stringValidationMsg = MifssMessages.getMessage("validation.empty.string");

        props = new PropertyListUi(poolPropertiesLabel, "150px", "200px");
        description = new TextField(poolDescriptionLabel);
        description.setWidth("90%");

        poolId = new TextField(poolIdLabel);
        poolId.setWidth("90%");

        implName = new ComboBox(poolImplLabel);
        implName.setWidth("300px");

        List<String> list = storageService.listAvailablePoolImplmentations();

        for (String s : list)
            implName.addItem(s);

        title = new TextField(poolTitleLabel);
        title.setWidth("90%");

        isWriteActive = new CheckBox(poolIsWriteActiveLabel);

        // setup validation
        description.addValidator(new EmptyStringValidator(stringValidationMsg));
        poolId.addValidator(new IntIdValidator(idValidationMsg));
        implName.addValidator(new EmptyStringValidator(stringValidationMsg));
        title.addValidator(new EmptyStringValidator(stringValidationMsg));

        addComponent(poolId);
        addComponent(title);
        addComponent(implName);
        addComponent(isWriteActive);
        addComponent(description);
        addComponent(props);

    }

}
